package com.zyk.leetcode;

import java.util.Arrays;

/**
 * @author zhangsan
 * @date 2021/4/27 9:55
 */
public class C34 {

    public static int[] searchRange(int[] nums, int target) {
        int l = 0, r = nums.length - 1, m;
        while (l <= r) {
            m = (l + r) >> 1;
            if (nums[m] < target) {
                l = m + 1;
            } else if (nums[m] > target) {
                r = m - 1;
            } else {
                int i = m, j = m;
                for (; i > 0 && nums[i] == nums[i - 1]; i--) {
                }
                for (; j < nums.length-1 && nums[j] == nums[j + 1]; j++) {
                }
                return new int[]{i, j};
            }
        }
        return new int[]{-1, -1};
    }


    // for test
    public static void main(String[] args) {
        int[] nums = {1};
        int target = 1;
        System.out.println(Arrays.toString(searchRange(nums, target)));
    }

}
